//
//  ConfirmationViewController.m
//  TURN-O-MATIC
//
//  Created by Josep on 12/15/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ConfirmationViewController.h"

@implementation ConfirmationViewController

@synthesize dni;
@synthesize dia;
@synthesize hora;
@synthesize indicadorActivitat;

@synthesize dniTextLabel;
@synthesize diaTextLabel;
@synthesize horaTextLabel;
@synthesize avisTextLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.dniTextLabel.text = self.dni;
    self.diaTextLabel.text = self.dia;
    self.horaTextLabel.text = self.hora;
    
}

- (void)viewDidUnload
{
    [self setDniTextLabel:nil];
    [self setDiaTextLabel:nil];
    [self setHoraTextLabel:nil];
    [self setIndicadorActivitat:nil];
    [self setAvisTextLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)confirmarReserva:(id)sender {
    self.indicadorActivitat.startAnimating;

    NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                           @"http://beto/php/controller.php?op=doreserva&dni=",self.dni,
                           @"&dia=", self.dia,
                           @"&hora=", self.hora];
    
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    
    if([newStr isEqualToString:@"1"]){
        //self.avisTextLabel.text = @"Reserva feta!";
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Reserva feta!"
                                                          message:@"La reserva s'ha efectuat correctament"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
    }else{
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No s'ha pogut reservar"
                                                          message:@"No s'ha pogut efectuar la reserva"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
        //self.avisTextLabel.text = @"No s'ha pogut reservar :(";
    }

    
    self.indicadorActivitat.stopAnimating;
}

@end
